package com.camin.android.adapters;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.camin.android.BaseActivity;
import com.camin.android.R;
import com.camin.android.RecipeActivity;
import com.camin.android.db.DataHelper;
import com.camin.android.db.RecipeTable;
import com.camin.android.utils.Constants;

public class RecipeAdapter extends CursorAdapter implements OnClickListener{

	private Cursor mCursor;
	private Context mContext;
	private final LayoutInflater mInflater;

	public RecipeAdapter(Context context, Cursor cursor) {
		super(context, cursor, true);
		this.mInflater = LayoutInflater.from(context);
		this.mContext = context;
		this.mCursor = cursor;
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		TextView t = (TextView) view.findViewById(R.id.txtRecipeName);
		t.setText(cursor.getString(cursor
				.getColumnIndex(RecipeTable.recipeName)));
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		final View view = mInflater.inflate(R.layout.recipe_list_item, parent,
				false);
		return view;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.recipe_list_item, parent,
					false);
		}

		View superView = super.getView(position, convertView, parent);
		if (mCursor.moveToPosition(position)) {
			TextView t = (TextView) convertView
					.findViewById(R.id.txtRecipeName);
			t.setText(mCursor.getString(mCursor
					.getColumnIndex(RecipeTable.recipeName)));
			t.setTag(new Integer(position));
			t.setOnClickListener(this);

			ImageView imgEditButton = (ImageView) convertView
					.findViewById(R.id.imgEditRecipeBtn);
			imgEditButton.setTag(new Integer(position));
			imgEditButton.setOnClickListener(this);

			ImageView imgDeleteButton = (ImageView) convertView
					.findViewById(R.id.imgDelRecipeBtn);
			imgDeleteButton.setTag(new Integer(position));
			imgDeleteButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View view) {
					mCursor.moveToPosition(position);
					String recipeName = mCursor.getString(mCursor
							.getColumnIndex(RecipeTable.recipeName));

					AlertDialog.Builder adb = new AlertDialog.Builder(mContext);
					adb.setTitle("Delete?");
					adb.setMessage("Are you sure you want to delete "
							+ recipeName);
					adb.setNegativeButton("Cancel", null);
					adb.setPositiveButton("Ok",
							new AlertDialog.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {

									Long recipeId = mCursor.getLong(mCursor
											.getColumnIndex(RecipeTable._ID));

									DataHelper dh = ((BaseActivity) mContext)
											.getDataHelper();
									dh.deleteRecipe(
											dh.getWritableDatabase(), recipeId);
									dh.deleteRecipeIngredient(
											dh.getWritableDatabase(), recipeId);

									mCursor.requery();
								}
							});
					adb.show();
				}
			});
		}
		return superView;
	}

	@Override
	public void onClick(View v) {
		mCursor.moveToPosition((Integer) v.getTag());
		Long recipeId = mCursor.getLong(mCursor
				.getColumnIndex(RecipeTable._ID));
		Intent intent = new Intent(((BaseActivity) mContext)
				.getBaseContext(), RecipeActivity.class);
		intent.putExtra(Constants.RECIPE_ID, recipeId);
		((BaseActivity) mContext).startActivityForResult(intent, 0);
	}

}
